<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Subscribe extends CI_Controller {

	function __construct(){
		parent::__construct();
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin");
		}
		$this->load->model('admin/subscribe_model','subscribe_model');
	}
	
	public function index($id=0,$p=1)
	{
		$this->lists($id,$p);
	}
	
	public function lists($p=1)
	{
		$menu['menu_1'] = '';
		$menu['menu_2'] = '';
		$menu['menu_3'] = '';
		$menu['menu_4'] = '';
		$menu['menu_5'] = 'current';
		
		$lists = $this->subscribe_model->lists(1,500);
		$data['lists'] = array();
		$i = 0;
		foreach($lists as $v){
			$data['lists'][$i]['id'] = $v->id;
			$data['lists'][$i]['name'] = $v->name;
			$data['lists'][$i]['email'] = $v->email;
			$data['lists'][$i]['status'] = $v->active?'active':'inactive';
			$i++;
		}
		
		$data['export_bt'] = '';
		if($i>0){
			$data['export_bt'] = '<a href = "'.base_url().'admin/subscribe/export" class = "add btn">Export</a>';
		}
		
		$this->template->set_template('admin');
		$this->template->write_view('menu','menu_view',$menu);
		$this->template->write_view('submenu','subscribe_submenu_view');
		$this->template->write_view('content','subscribe_view',$data);
		$this->template->render();
	}
	
	public function status($id)
	{
		if(($id+0)>0){
			$this->subscribe_model->status($id+0);
		}
		redirect(getenv("HTTP_REFERER"));
	}
	
	public function del($id)
	{
		if(($id+0)>0){
			$this->subscribe_model->delete($id+0);
			redirect(getenv("HTTP_REFERER"));
		}else{
			redirect(base_url()."admin/subscribe");
		}
	}
	
	public function export()
	{
		$data['list'] = $this->subscribe_model->lists(1,50000);
		$this->template->set_template('blank');
		$this->template->write_view('content','subscribe_csv_view',$data);
		$this->template->render();
	}
	
	public function export2()
	{
		$lists = $this->subscribe_model->lists(1,50000);
		
		$emailfile = APPPATH.'../assets/subscribe.csv';
		$objWrite = fopen($emailfile, "w");
		$i = 1;
		foreach($lists as $v){
			if($v->active=='1')
			{
				$confirm = 'YES';
			}
			else
			{
				$confirm = 'NO';
			}
			fwrite($objWrite, "\"".($i++)."\",\"".utf8_decode($v->name).'","'.$v->email."\",\"".$confirm."\" \n");
		}
		fclose($objWrite);
		redirect(site_url().'admin/subscribe/download');
	}
	
	public function download()
	{
		header("Pragma: public");
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Content-Type: application/force-download");
		header("Content-Type: application/octet-stream");
		header("Content-Type: application/download");;
		header("Content-Disposition: attachment;filename=subscribe.csv"); // ª×èÍä¿Åì .XLS ÊÓËÃÑº´ÒÇ¹ìâËÅ´
		header("Content-Transfer-Encoding: binary ");
		      
		$emailfile = APPPATH.'../assets/subscribe.csv';
				
		$row = 1;
		if (($handle = fopen($emailfile, "r")) !== FALSE) {
			while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
				$num = count($data);
				$row++;
				for ($c=0; $c < $num; $c++) {
					echo $data[$c].',' ;
				}
				echo "\n";
			}
			fclose($handle);
		}
	}
}

/* End of file subscribe.php */
/* Location: ./modules/admin/controllers/subscribe.php */